package com.itboye.ihomebank.activity.electric_meter;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.ElectricMeterBean;
import com.itboye.ihomebank.bean.MeterDetailBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.ElectricMeterPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;

/**
 * Created by Administrator on 2018/8/24 0024.
 */

public class ActivitySmartMeter extends BaseOtherActivity {
    View v_statusbar;
    ImageView close_icon;
    TextView add_shap_title_tv,save;

    LinearLayout addressLl;
    TextView addressTv;
    TextView commitTv;//充值
    ImageView more;
    static int REQUESTCODE=803;
    String  uid;

    TextView eletricValue;  //本月消费 电量
    ImageView disconnectIv; //断开
    TextView amountValue;//剩余电量
    TextView feeValue;//剩余电费
    LinearLayout brokenLine;//电费走势
    MeterDetailBean meterBean;
    ElectricMeterPresenter electricMeterPresenter;
    int position;
    ElectricMeterPresenter presenter;
    List<MeterDetailBean> meters;
    private String xiaoquId;
    @Override
    protected int layout_id() {
        return R.layout.activity_smartmeter;
    }

    @Override
    protected void onInitController() {

    }

    @Override
    protected void onInit() {

        setStatusBarHeight(v_statusbar);
        add_shap_title_tv.setText("智能电表");
        save.setVisibility(View.VISIBLE);
        save.setText("账单");
        presenter = new ElectricMeterPresenter(this);


        meters=new ArrayList<>();
        uid = SPUtils.get(this,null, SPContants.USER_ID,"")+"";
        electricMeterPresenter=new ElectricMeterPresenter(this);

//        intent1.putExtra("emeterBean",meterBean);
//        intent1.putExtra("emeterSize",meterBean.getHouses().size());
//        intent1.putExtra("position" ,0);
        xiaoquId=getIntent().getStringExtra("xiaoqu_id");
        position=getIntent().getIntExtra("position",0);
        meterBean = (MeterDetailBean) getIntent().getSerializableExtra("meter_detail");
        initData(meterBean);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        presenter.queryMyEmeter(uid,"","");
    }

    //初始化界面数据
    private void initData(MeterDetailBean meterBean) {
        addressTv.setText("房屋地址:"+meterBean.getAddress());
        if (meterBean.getValue_month_public().equals("")||Double.parseDouble(meterBean.getValue_month_public())!=0){
            eletricValue.setText(Double.parseDouble(meterBean.getValue_month())+"度");
        }else {
            eletricValue.setText(Double.parseDouble(meterBean.getValue_month())+Double.parseDouble(meterBean.getValue_month_public())+"度");
        }
        amountValue.setText(meterBean.getValue_remain());

        DecimalFormat df;

        if(Double.parseDouble(meterBean.getBalance())/100<1){
            df = new DecimalFormat("#0.00");
        }else {
            df = new DecimalFormat("#.00");
        }

        feeValue.setText(df.format(Double.parseDouble(meterBean.getBalance())/100)+"元");

        //电表运行状态
//        if (Double.parseDouble(meterBean.getValue_unused())<=0){ //已断开
//         eletricValue.setVisibility(View.GONE);
//         disconnectIv.setVisibility(View.VISIBLE);
//        }else {
//         eletricValue.setVisibility(View.VISIBLE);
//         disconnectIv.setVisibility(View.GONE);
//        }

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void update(Observable observable, Object data) {
        super.update(observable, data);
        ResultEntity entity = handlerError(data);
        if (entity!=null){
            if (entity.getEventType() == ElectricMeterPresenter.BY_Emeter_address_success){ //我的列表

                if (meters.size()>0){
                    meters.clear();
                }
                List<ElectricMeterBean>  meterBeans= (List<ElectricMeterBean>) entity.getData();
                if (meterBeans.size()>0){
                    for (int i=0;i<meterBeans.size();i++){
                        for (int j=0;j<meterBeans.get(i).getEmeter_list().size();j++){
                            meterBeans.get(i).getEmeter_list().get(j).setAddress(meterBeans.get(i).getAddress()+"_"+meterBeans.get(i).getEmeter_list().get(j).getName());
                            meters.add(meterBeans.get(i).getEmeter_list().get(j));
                        }
                    }
                }
                meterBean=meters.get(position);
                initData(meterBean);
            }else  if (entity.getEventType() == ElectricMeterPresenter.BY_Emeter_address_fail){

                ByAlert.alert(entity.getMsg());
            }
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);

        switch (v.getId()){

            case R.id.close_icon://返回
                this.finish();
                break;

            case R.id.save: //账单
                startActivity(new Intent(this,ActivityElectricBill.class));
                break;

            case R.id.commitTv: //电费充值
                Intent intent = new Intent(this, ActivityRecharge.class);
                intent.putExtra("item",meterBean.getId());
                startActivity(intent);
                break;

            case R.id.brokenLine:
                Intent intent1 = new Intent(this, ActivityElecTariffTrend.class);
                intent1.putExtra("aid",xiaoquId);
                startActivity(intent1);
                break;

        }
    }

}
